package com.kmxd.ams.core.util;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SmUtil;
import com.kmxd.ams.core.common.constant.ErrorCode;
import com.kmxd.ams.core.common.enums.SecureEnum;
import com.kmxd.ams.core.exception.BizException;

import java.io.File;

/**
 * 文件校验工具类
 *
 * @author nickbi
 */
public class FileCheckUtil {
  public static String getVerifyCode(String filePath, SecureEnum type) {
    File file = new File(filePath);
    return getVerifyCode(file, type);
  }

  public static String getVerifyCode(File file, SecureEnum type) {
    switch (type) {
      case MD5:
        return SecureUtils.md5(file);
      case SM3:
        return SmUtil.sm3(file);
      case SHA1:
        return SecureUtil.sha1(file);
      case SHA256:
        return SecureUtil.sha256(file);
      default:
        throw BizException.instance(ErrorCode.NOT_FOUND, "未找到加密类型");
    }
  }

  public static String getVerifyCode(String filePath) {
    return getVerifyCode(filePath, SecureEnum.MD5);
  }

  /**
   * 文件校验
   *
   * @param filePath 文件地址
   * @param verifyCode 校验码
   * @param type 校验类型
   * @return 结果
   */
  public static boolean check(String filePath, String verifyCode, SecureEnum type) {
    String code = getVerifyCode(filePath, type);
    return code.equals(verifyCode);
  }

  public static boolean check(File file, String verifyCode, SecureEnum type) {
    String code = getVerifyCode(file, type);
    return code.equals(verifyCode);
  }
}
